//
//  MessageTestGPWViewController.m
//  carcareIOS
//
//  Created by 王然 on 16/6/28.
//  Copyright © 2016年 chezheng. All rights reserved.
//

#import "MessageTestGPWViewController.h"
#import "UserHelper.h"
#import "NetSetting.h"
#import "SettingMgr.h"
#import "WRGestureViewController.h"
#define WrongL_Height (27)
#define PhoneV_Height (65)
#define Wrong_Word_Color (COLOR_HEXSTRING(@"#f05f33"))

@interface MessageTestGPWViewController ()

@property (nonatomic , strong) UILabel * wrongL;//错误提示label
@property (nonatomic , strong) UILabel * phoneNumL;//电话号label
@property (nonatomic , strong) UIButton * nextButton;//下一步按钮
@property (nonatomic , strong) UIButton * codeButton;//获取验证码按钮
@property (nonatomic , strong) UITextField * codeTextField;//验证码textfield
@property (nonatomic , strong) UIButton *callMessageButton;

//验证是否获取验证码并验证成功 返回上一个页面
@property (nonatomic , copy) void (^testSussess)();

@end

@implementation MessageTestGPWViewController
- (instancetype)initWithMainViewType:(TYPE_MAIN_VIEW)type withTestSuccess:(void(^)())success
{
    self = [super initWithMainViewType:type];
    if (self) {
        if(success) self.testSussess = success;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [Theme backGroundColor];
    
    self.title = @"短信验证";
    __weak __typeof(self) wself = self;
//    [self navSetupLeftTitle:@"取消" action:^{
//        [wself dismissViewControllerAnimated:YES completion:nil];
//    }];
    
    self.wrongL = [[[[UILabel labelWithFrame:CGRectMake(0, 0, __MAIN_WIDTH, WrongL_Height) font:FONT_DEFAULT_Light(13) text:@"验证码输入错误" textColor:Wrong_Word_Color] setupOnView:self.view] resetBackgroundColor:COLOR_HEXSTRING(@"#fedfd7")] resetBorderWidth:0.5 borderColor:Wrong_Word_Color];
    [self.wrongL setTextAlignment:NSTextAlignmentCenter];
    [self.wrongL setHidden:YES];
    
    self.phoneNumL = [[UILabel labelWithFrame:CGRectMake(10, WrongL_Height, __MAIN_WIDTH - 10 * 2, PhoneV_Height) font:FONT_DEFAULT_Light(25) text:USERINFO.tel textColor:[Theme mainNavColor]] setupOnView:self.view];
    [self.phoneNumL setTextAlignment:NSTextAlignmentCenter];
    
    CGFloat codeTFX = 37 * __MAIN_RATIO_375;
    UIView * codeBackV = [[UIView viewWithFrame:CGRectMake(0, self.phoneNumL.bottom, __MAIN_WIDTH, 50)] setupOnView:self.view];
    [codeBackV setBackgroundColor:[UIColor whiteColor]];
    self.codeTextField = [[UITextField alloc]initWithFrame:CGRectMake(codeTFX, 0,( __MAIN_WIDTH - codeTFX)/ 3 * 2, codeBackV.frame.size.height)];
    self.codeTextField.placeholder = @"请输入短信验证码";
    [codeBackV addSubview:self.codeTextField];
    self.codeTextField.font = FONT_DEFAULT_Light(16);
    self.codeTextField.textColor = COLOR_HEXSTRING(@"#323232");
    self.codeTextField.textAlignment = NSTextAlignmentLeft;
    self.codeTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.codeTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [self.codeTextField becomeFirstResponder];
//    [self registerTextField:self.codeTextField];
    [[[UIView viewWithFrame:CGRectMake(0, 0, __MAIN_WIDTH , 0.5 )] resetBackgroundColor:COLOR_HEXSTRING(@"#DCDCDC")] setupOnView:codeBackV];
    
    [[[UIView viewWithFrame:CGRectMake(0, codeBackV.frame.size.height - 0.5, __MAIN_WIDTH, 0.5)] resetBackgroundColor:COLOR_HEXSTRING(@"#DCDCDC")] setupOnView:codeBackV];
    
    // 2090E6
    // 登录按钮
    CGFloat nextButtonX = 16 * __MAIN_RATIO_375;
    CGFloat nextButtonHeight = 45;
    self.nextButton = [ViewFactory buttonWithFrame:CGRectMake(nextButtonX, codeBackV.bottom + 25 * __MAIN_RATIO_H_667,__MAIN_WIDTH - nextButtonX * 2, nextButtonHeight)  title:@"下一步" normalColor:COLOR_HEXSTRING(@"#1F90E5") highlightColor:COLOR_HEXSTRING(@"#1F90E5") disabledColor:COLOR_HEXSTRING(@"cbcbcb") inView:self.view action:^{
        [wself clickOnBG];
        [wself clickOnNext];
    }];
    self.nextButton.clipsToBounds = YES;
    self.nextButton.layer.cornerRadius = 2;
    self.nextButton.enabled = NO;
    
    // 收不到短信验证码
    CGFloat callButtonWidth = 250 * __MAIN_RATIO_375;
    CGFloat callButtonHeight = 14 * __MAIN_RATIO_H_667;
    
    __block UILabel *callMessageLabel = [Factory labelWithFrame:CGRectMake(self.nextButton.left, self.nextButton.bottom + 18 * __MAIN_RATIO_H_667, callButtonWidth, callButtonHeight) font:FONT_DEFAULT_Light(12) text:@"" textColor:COLOR_HEXSTRING(@"#a4a4a4") onView:self.view textAlignment:NSTextAlignmentLeft];
    
    
    NSString * kongStr = (__MAIN_WIDTH == 320 ? @"":@" ");
    NSString *callMessageStr = [NSString  stringWithFormat:@"收不到短信验证码？%@请点击这里",kongStr];
    NSMutableAttributedString * CMAttributeStr = [[NSMutableAttributedString alloc]initWithString:callMessageStr];
    NSRange CMRange = NSMakeRange([callMessageStr length] - 5, [callMessageStr length] - ([callMessageStr length] - 5));
    [CMAttributeStr addAttribute:NSForegroundColorAttributeName value:COLOR_HEXSTRING(@"#1F90E5") range:CMRange];
    [CMAttributeStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:CMRange];
    //    [CMAttributeStr addAttribute:NSKernAttributeName value:[NSNumber numberWithInteger: 2 * __MAIN_RATIO_375] range:NSMakeRange(0, [CMAttributeStr length])];
    NSMutableParagraphStyle * CMparegraphStyle = [[NSMutableParagraphStyle alloc]init];
    [CMparegraphStyle  setParagraphSpacing:5];
    [CMAttributeStr addAttribute:NSParagraphStyleAttributeName value:CMparegraphStyle range:CMRange];
    callMessageLabel.attributedText = CMAttributeStr;
    callMessageLabel.hidden = YES;
    
    
    self.callMessageButton = [[UIButton buttonWithFrame:CGRectMake(__MAIN_WIDTH - 22 * __MAIN_RATIO_375 - callButtonWidth, self.nextButton.bottom + 20 * __MAIN_RATIO_H_667, callButtonWidth, 30 * __MAIN_RATIO_H_667) title:@"" textColor:COLOR_HEXSTRING(@"#a4a4a4") font:FONT_BUTTON click:^{
        //确认 打电话纪录语音信息  取消  不做处理
        TipsView *tipsV = [TipsView voiceTipsWithTitle:nil lightTitles:@[@"验证码将会以电话方式播报给您，请注意来电，并记下电话中报的验证码"]];
        [tipsV addLeftButton:@"取消" clickLeft:^{
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
        } rightButton:@"获取验证码" clickRight:^{
            [UserHelper callMessageCodeByTel: wself.phoneNumL.text success:nil fail:nil];
            [AlertMgr dismiss:TYPE_VIEW_ALERT];
        }];
        [AlertMgr showView:tipsV];
    }] setupOnView:self.view];
    
    wself.callMessageButton.hidden = YES;
    
    // 验证码
    
    self.codeButton = [ViewFactory addButtonType:BUTTON_CODE onTextField:self.codeTextField inView:codeBackV action:^{
        [wself.codeTextField becomeFirstResponder];
//        wself.simulationButton.top = wself.okButton.bottom + 45 + callMessageLabel.height;
        wself.callMessageButton.hidden = NO;
        callMessageLabel.hidden = NO;
        wself.codeButton.enabled = NO;
        [UserHelper getCodeByTel:wself.phoneNumL.text success:nil fail:^{
            wself.codeButton.enabled = YES;
        }];
    } ok:nil];
    
    [[[UIView viewWithFrame:CGRectMake(self.codeButton.left - (__MAIN_WIDTH - self.codeButton.right), 8, 0.5, codeBackV.frame.size.height - 8 * 2)] resetBackgroundColor:COLOR_HEXSTRING(@"#DCDCDC")] setupOnView:codeBackV];
}
#pragma mark - 下一步按钮
- (void)clickOnNext
{
    if([Check isCode:self.codeTextField.text]){
        
        __weak __typeof(self) wself = self;
        [self.view endEditing:YES];
        [[NetSetting getTestGestureMsgCode:self.codeTextField.text]requestSuccess:^(id JSON) {
            
            [SETTINGMGR setGesturePasswordFlag:SETTINGMGR.showGesturePassword withCanLookGesturePWFLag:YES withGesturePWStr:@""];
            
            [wself.navigationController popViewControllerAnimated:YES];
            [wself.wrongL setHidden:YES];
        
            if (self.testSussess) self.testSussess();
            
        } failure:^(id JSON) {
            [wself.wrongL setHidden:NO];
        }];
    }
}
-(void)textFieldTextChanged:(UITextField *)textField
{

    self.nextButton.enabled = ( [self.codeTextField.text isCode] ? YES : NO);
     
}

- (NSInteger)textFieldMaxNumberWithTextField:(UITextField *)textField
{
    if (textField == self.codeTextField)
    {
        return 5;
    }
    else{
        return [super textFieldMaxNumberWithTextField:textField];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self clickOnBG];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
